package com.heima.article.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.heima.api.dto.ArticleHomeDto;
import com.heima.api.entity.ApArticle;
import com.heima.api.entity.ApArticleConfig;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.IApArticleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-08-12
 */
@Service
@RequiredArgsConstructor
public class ApArticleServiceImpl extends MPJBaseServiceImpl<ApArticleMapper, ApArticle> implements IApArticleService {
    private final ApArticleMapper apArticleMapper;

    @Override
    public List<ApArticle> load(ArticleHomeDto articleHomeDto, int type) {
        MPJLambdaWrapper<ApArticle> wrapper = new MPJLambdaWrapper<ApArticle>()
                .leftJoin(ApArticleConfig.class, ApArticleConfig::getArticleId, ApArticle::getId)
                .ne(ApArticleConfig::getIsDelete, 1)
                .ne(ApArticleConfig::getIsDown, 1)
                .lt(type == 1, ApArticle::getPublishTime, articleHomeDto.getMinBehotTime())
                .gt(type == 2, ApArticle::getPublishTime, articleHomeDto.getMaxBehotTime())
                .eq(!articleHomeDto.getTag().equals("__all__"), ApArticle::getChannelId, articleHomeDto.getTag())
                .orderByDesc(ApArticle::getPublishTime);
        Page<ApArticle> page = new Page<>(1, articleHomeDto.getSize());
        Page<ApArticle> apArticlePage = apArticleMapper.selectPage(page, wrapper);
        List<ApArticle> apArticleList = BeanUtil.copyToList(apArticlePage.getRecords(), ApArticle.class);
        return apArticleList;
    }
}
